package com.pk.android.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.pk.android.R;
import com.pk.android.dummy.temp_source;
import com.pk.android.model.BaseFragment;
import com.pk.android.view.adapter.LeagueTeamsAdapter;

public class MyLeaguesFragment extends BaseFragment {
 
	public static final String EXTRA_TITLE = "EXTRA_TITLE";
	OnAllLeaguesClickListener mCallback;
	
	LeagueTeamsAdapter teamsAdapter;
	Button btnAllLeagues;

	// Container Activity must implement this interface
    public interface OnAllLeaguesClickListener {
        public void onAllLeaguesClick(int position);
    }
    
	public static final MyLeaguesFragment newInstance(String title)
	{
		MyLeaguesFragment f = new MyLeaguesFragment();
		Bundle bdl = new Bundle(1);
		bdl.putString(EXTRA_TITLE, title);
	    f.setArguments(bdl);

	    f.setTitle(title);
	    
		return f;
	}
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnAllLeaguesClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnAllLeaguesClickListener");
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	super.onCreateView(inflater, container, savedInstanceState);
    	
        view = inflater.inflate(R.layout.fragment_pk_my_leagues, container, false);
        
    	((TextView)view.findViewById(R.id.txt_league_name)).setText(title); 
    	
        return view;
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
		super.onCreate(savedInstanceState);
	}


	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		btnAllLeagues = (Button)view.findViewById(R.id.btn_all_leagues);
		btnAllLeagues.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				mCallback.onAllLeaguesClick(1);
			}});
		

		ListView listViewTeams = (ListView)view.findViewById(R.id.team_list);
		teamsAdapter = new LeagueTeamsAdapter(getActivity(), temp_source.getMyTeamList());
		listViewTeams.setAdapter(teamsAdapter);
	}
	
}
